package com.wyix.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wyix.common.BusinessException;
import com.wyix.common.PageResult;
import com.wyix.domain.dto.StudentDTO;
import com.wyix.domain.dto.StudentQueryDTO;
import com.wyix.domain.entity.StudentDO;
import com.wyix.mapper.StudentMapper;
import com.wyix.service.StudentService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 学生服务实现类
 */
@Slf4j
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    /**
     * 新增学生
     */
    @Override
    @Transactional
    public void addStudent(StudentDTO studentDTO) {
        log.info("新增学生: {}", studentDTO);

        // 检查学号是否已存在
        StudentDO existingStudent = studentMapper.selectByStudentNo(studentDTO.getStudentNo());
        if (existingStudent != null) {
            log.warn("学号已存在: {}", studentDTO.getStudentNo());
            throw new BusinessException("学号已存在");
        }

        // 转换DTO为实体
        StudentDO student = new StudentDO();
        BeanUtils.copyProperties(studentDTO, student);

        // 执行新增
        int rows = studentMapper.insert(student);
        if (rows != 1) {
            log.error("新增学生失败: {}", studentDTO);
            throw new BusinessException("新增学生失败");
        }

        log.info("新增学生成功，ID: {}", student.getId());
    }

    /**
     * 根据ID删除学生
     */
    @Override
    @Transactional
    public void deleteStudent(Long id) {
        log.info("删除学生，ID: {}", id);

        // 检查学生是否存在
        StudentDO student = studentMapper.selectById(id);
        if (student == null) {
            log.warn("删除学生失败，学生不存在，ID: {}", id);
            throw new BusinessException("学生不存在");
        }

        // 执行删除
        int rows = studentMapper.deleteById(id);
        if (rows != 1) {
            log.error("删除学生失败，ID: {}", id);
            throw new BusinessException("删除学生失败");
        }

        log.info("删除学生成功，ID: {}", id);
    }

    /**
     * 更新学生信息
     */
    @Override
    @Transactional
    public void updateStudent(StudentDTO studentDTO) {
        log.info("更新学生: {}", studentDTO);

        // 检查ID是否存在
        if (studentDTO.getId() == null) {
            log.warn("更新学生失败，ID不能为空");
            throw new BusinessException("ID不能为空");
        }

        // 检查学生是否存在
        StudentDO existingStudent = studentMapper.selectById(studentDTO.getId());
        if (existingStudent == null) {
            log.warn("更新学生失败，学生不存在，ID: {}", studentDTO.getId());
            throw new BusinessException("学生不存在");
        }

        // 检查学号是否已被其他学生使用
        if (studentDTO.getStudentNo() != null && !studentDTO.getStudentNo().equals(existingStudent.getStudentNo())) {
            StudentDO studentByNo = studentMapper.selectByStudentNo(studentDTO.getStudentNo());
            if (studentByNo != null) {
                log.warn("更新学生失败，学号已存在: {}", studentDTO.getStudentNo());
                throw new BusinessException("学号已存在");
            }
        }

        // 转换DTO为实体
        StudentDO student = new StudentDO();
        BeanUtils.copyProperties(studentDTO, student);

        // 执行更新
        int rows = studentMapper.updateById(student);
        if (rows != 1) {
            log.error("更新学生失败: {}", studentDTO);
            throw new BusinessException("更新学生失败");
        }

        log.info("更新学生成功，ID: {}", studentDTO.getId());
    }

    /**
     * 根据ID查询学生
     */
    @Override
    public StudentDO getStudentById(Long id) {
        log.info("查询学生，ID: {}", id);

        if (id == null) {
            log.warn("查询学生失败，ID不能为空");
            throw new BusinessException("ID不能为空");
        }

        StudentDO student = studentMapper.selectById(id);
        if (student == null) {
            log.warn("查询学生失败，学生不存在，ID: {}", id);
            throw new BusinessException("学生不存在");
        }

        log.info("查询学生成功，ID: {}", id);
        return student;
    }

    /**
     * 多条件分页查询学生
     */
    @Override
    public PageResult<StudentDO> queryStudents(StudentQueryDTO queryDTO) {
        log.info("查询学生列表，条件: {}", queryDTO);

        // 分页查询
        PageHelper.startPage(queryDTO.getPageNum(), queryDTO.getPageSize());
        Page<StudentDO> page = (Page<StudentDO>) studentMapper.selectByCondition(queryDTO);

        // 封装分页结果
        PageResult<StudentDO> pageResult = new PageResult<>(
                page.getTotal(),
                page.getPages(),
                page.getPageNum(),
                page.getPageSize(),
                page.getResult()
        );

        log.info("查询学生列表成功，总记录数: {}", page.getTotal());
        return pageResult;
    }

    /**
     * 查询所有学生
     */
    @Override
    public List<StudentDO> getAllStudents() {
        log.info("查询所有学生");
        List<StudentDO> students = studentMapper.selectAll();
        log.info("查询所有学生成功，数量: {}", students.size());
        return students;
    }
}
